<template>
	<view v-show="visible">
		<!-- 使用z-paging-swiper为根节点可以免计算高度 -->
		<z-paging-swiper>
			<!-- 需要固定在顶部不滚动的view放在slot="top"的view中 -->
			<!-- 注意！此处的z-tabs为独立的组件，可替换为第三方的tabs，若需要使用z-tabs，请在插件市场搜索z-tabs并引入，否则会报插件找不到的错误 -->
			<template #top>
				<view class="Grid Grid--center bg-white" style="padding: 0 8px;">
					<!-- <view class="layout-padding-tb-12 orderNum">
						<view class="orderNum-item">
							<view class="num">{{orderNumTotal['article'] || 0}}/{{orderNumTotal['sales'] || 0}}</view>
							<view class="name">发布数/总销量</view>
						</view>
						<view class="orderNum-item">
							<view class="num">{{orderNumTotal['views'] || 0}}</view>
							<view class="name">总浏览量</view>
						</view>
						<view class="orderNum-item">
							<view class="num">
								<u--text align="center" color="#0074FF" bold mode="price" :text="orderNumTotal['amount'] || 0" :size="12"></u--text>
							</view>
							<view class="name">销售总额</view>
						</view>
						<view class="orderNum-item">
							<view class="num">
								<u--text align="center" color="#0074FF" bold mode="price" :text="orderNumTotal['pay_amount'] || 0" :size="12"></u--text>
							</view>
							<view class="name">实收总额</view>
						</view>
					</view> -->
					<view v-if="userInfo.role_ids.includes(1) || userInfo.role_ids.includes(3) || userInfo.role_ids.includes(4)">
						<!-- <u-button :hairline="false" :icon="isQueryField ? 'arrow-up' : 'arrow-down'" plain :text="getText"
							style="border: none !important;" @click="isQueryField = true"></u-button> -->
						<u--text :lines="1" :suffixIcon="!isUsers ? 'arrow-down' : 'arrow-up'" color="#222222" iconStyle="font-size: 14px; padding: 3px 5px 0 2px; color: #222222" size="14" :text="user_obj.nickname" style="max-width: 80px;" @click="handleUserList"></u--text>
					</view>
					<u-search class="Grid-cell layout-padding-tb-12" placeholder="请输入搜索内容" bgColor="#F7F7F7" searchIconColor="#999999" placeholderColor="#999999" v-model="keyword" @clear="search"
						@custom="search" :action-style="{color: '#52B36D'}" bg-color="#FAFAFB"
						placeholder-color="#BBBBBC" search-icon-color="#BBBBBC"></u-search>
				</view>
				<z-tabs unit="px" ref="tabs" scroll-count="1" bar-width="76" bar-height="5" :list="tabList" :current="current" @change="tabsChange"
					:active-style="{color: '#52B36D'}" :inactive-style="{color: '#666666'}" active-color="#52B36D"
					bottom-space="0" tab-width="76px" />
			</template>
			<!-- swiper必须设置height:100%，因为swiper有默认的高度，只有设置高度100%才可以铺满页面  -->
			<swiper v-if="isView" class="swiper" :current="current" @transition="swiperTransition"
				@animationfinish="swiperAnimationfinish">
				<swiper-item class="swiper-item" v-for="(item, index) in tabList" :key="index">
					<!-- 这里的swiper-list-item为demo中为演示用定义的组件，列表及分页代码在swiper-list-item组件内 -->
					<!-- 请注意，swiper-list-item非z-paging内置组件，在自己的项目中必须自己创建，若未创建则会报组件不存在的错误 -->
					<swiper-list-item ref="listItem" componentCurrent="homeItem" :tabIndex="index"
						:clickIndex="clickIndex" :currentIndex="current" @initData="loadData"></swiper-list-item>
				</swiper-item>
			</swiper>

			<!-- <poster ref="poster"></poster> -->

			<!-- 设置折扣 -->
			<u-popup :show="discountShow" @close="discountShow = false" closeable mode="center" safeAreaInsetTop
				:closeOnClickOverlay="false">
				<view style="padding: 12px 10px;">设置折扣</view>
				<scroll-view scroll-y="true" style="max-height: 300px; padding: 0 10px; box-sizing: border-box;">
					<u--form class="custom-form" labelPosition="top" labelWidth="100" :labelStyle="formLabelStyle"
						:model="form" ref="uForm" :rules="rules" errorType="message">
						<u-row v-for="(item, index) in form.discountList" :key="index" style="margin-bottom: 10px;">
							<u-col span="5">
								<u-form-item label="价格" :prop="`discountList.${index}.price`" required
									labelPosition="top">
									<u--input v-model="item.price" type="digit" placeholder="请输入折后价格"
										border="none"></u--input>
								</u-form-item>
							</u-col>
							<u-col span="5">
								<u-form-item label="关联用户" :prop="`discountList.${index}.user_ids`" required
									labelPosition="top">
									<view style="width: 100%;" @click="relation(index)">{{item.user_ids.length
									? `含[${item.user_ids[0].nickname}...] 共${item.user_ids.length}人`
									: '请选择用户'}}</view>
								</u-form-item>
							</u-col>
							<!-- <u-col span="1">
								<u-icon name="plus" color="" size="14"></u-icon>
							</u-col> -->
							<u-col span="2">
								<u-icon v-if="form.discountList.length !== 1" name="minus" color="red" size="14" @click="() => {
									form.discountList.splice(index, 1)
									setRules()
								}"></u-icon>
							</u-col>
						</u-row>
					</u--form>
				</scroll-view>

				<view class="Grid Grid--1of2" style="margin-bottom: 10px;">
					<view class="Grid-cell" style="padding: 0 4px;">
						<u-button text="追加折扣" @click="() => {
								form.discountList.push({
									price: '',
									user_ids: []
								  })
								setRules()
							}"></u-button>
					</view>
					<view class="Grid-cell" style="padding: 0 4px;">
						<u-button type="primary" text="确认" @click="handleDiscountOk"></u-button>
					</view>
				</view>

			</u-popup>

			<!-- 选择用户 -->
			<u-popup :show="fansShow" @close="fansShow = false" closeable mode="right" safeAreaInsetTop
				:closeOnClickOverlay="false" :zIndex="10076" :customStyle="{padding: '15px 15px 5px'}">
				<view class="text-base" style="margin-bottom: 5px;">选择用户</view>
				<view class="Grid Grid--justifyCenter Grid--center">
					<view>
						<u-button :hairline="false" :icon="isQueryField ? 'arrow-up' : 'arrow-down'" plain
							:text="getText" style="border: none !important;" @click="isQueryField = true"></u-button>
					</view>
					<view class="Grid-cell">
						<u-search placeholder="请输入关键字" v-model="fansKeyword" shape="square" @search="() => {}" @custom="() => {
							query.page = 1
							fansData = []
							getFansList()
						}" style="width: 180px;"></u-search>
					</view>
				</view>
				<view class="text-xs" style="padding: 10px 0;">
					<scroll-view scroll-y="true" @scrolltolower="() => {
						getFansList()
					}" style="height: 75vh;">
						<u-checkbox-group v-model="fansCheck" placement="column" borderBottom iconPlacement="right">
							<u-checkbox v-for="(item, index) in fansData" activeColor="rgb(0, 116, 255)"
								:label="item.text" :name="item.value" style="margin-bottom: 5px;"
								@change="(e) => fansChange(e, item)"></u-checkbox>
						</u-checkbox-group>
					</scroll-view>
					<!-- <view style="position: absolute; bottom: 0; left: 0; width: 100%; padding: 5px;">
						<u-button type="primary" text="确定" @click="handleInvisibleIds"></u-button>
					</view> -->
				</view>
				<!-- 用户列表筛选条件选择 -->
				<u-action-sheet :show="isQueryField" safeAreaInsetBottom cancelText="取消"
					:actions="[{name: '会员ID', key: 'member_id'}, {name: '昵称', key: 'nickname'}]" title="请选择"
					description="" @close="isQueryField = false" @select="(e) => {
						invisibleKeyword = ''
						query = {
							nickname: '',
							member_id: '',
							wechat_id: '',
							status: 1,
							page: 1,
							limit: 20,
							not_in: []
						}
						fansData = []
						queryField = e.key
						getFansList()
					}">
				</u-action-sheet>
			</u-popup>
			
			<!-- 组员信息 -->
			<u-popup :show="isUsers" @close="isUsers = false" closeable mode="right" safeAreaInsetTop>
				<view class="text-xs" style="width: 70vw; padding: 0 15px;">
					<!-- 虚拟列表 -->
					<!-- 如果页面中的cell高度是固定不变的，则不需要设置cell-height-mode，如果页面中高度是动态改变的，则设置cell-height-mode="dynamic" -->
					<z-paging unit="px" ref="userListPaging" :fixed="false" :height="zPagingHeight" use-virtual-list>
						<!-- 通过slot="cell"插入列表for循环的cell，slot-scope中提供当前for循环的item和index -->
						<!-- 因字节跳动小程序不支持slot-scope，因此不支持字节跳动小程序 -->
						<view slot="cell" slot-scope="{item,index}">
							<view class="bg-white press-down-active" style="padding: 5px 0; margin-bottom: 10px; border-radius: 4px;" @click="() => {
								user_obj = item
								reloadCurrentList()
								isUsers = false
							}">
								<view class="Media">
									<u--image shape="circle" :src="item.avatar_url" width="45px" height="45px" />
									<view class="Media-body text-xs" style="padding: 0 10px;">
										<view class="Grid">
											<view style="margin-bottom: 10px;">ID:{{item.user_id}}</view>
										</view>
										<view class="Grid">
											<view style="max-width: 100px;">{{item.nickname}}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</z-paging>
				</view>
			</u-popup>
			<!-- 浏览记录 -->
			<u-popup :show="isContentLog" @close="isContentLog = false" closeable mode="right" safeAreaInsetTop>
				<view class="text-xs" style="width: 70vw; padding: 0 15px;">
					<!-- 虚拟列表 -->
					<!-- 如果页面中的cell高度是固定不变的，则不需要设置cell-height-mode，如果页面中高度是动态改变的，则设置cell-height-mode="dynamic" -->
					<z-paging unit="px" ref="contentLogPaging" :fixed="false" :height="zPagingHeight" use-virtual-list @query="getContentLog">
						<!-- 通过slot="cell"插入列表for循环的cell，slot-scope中提供当前for循环的item和index -->
						<!-- 因字节跳动小程序不支持slot-scope，因此不支持字节跳动小程序 -->
						<view slot="cell" slot-scope="{item,index}">
							<view class="bg-white press-down-active" style="padding: 5px 0; margin-bottom: 10px; border-radius: 4px;">
								<view class="Media">
									<view style="width: 45px; height: 45px; position: relative;">
										<u--image shape="circle" :src="item.headimgurl" width="45px" height="45px" @click="() => {
											data = item
											$refs.xfmx.open()
										}" />
										<u--image v-if="item.is_subscribe" shape="circle" :src="wx_png" width="24" height="24" style="position: absolute; bottom: 0; right: 0;" />
									</view>
									<view class="Media-body text-xs" style="padding: 0 10px;">
										<view class="Grid">
											<view style="margin-bottom: 10px;">ID:{{item.member_id}}</view>
											<view v-if="!item.pay_status" class="Grid-cell text-right">未支付</view>
											<view v-if="item.pay_status === 1" class="Grid-cell text-right" style="color: #5ac725;">已支付</view>
											<view v-if="item.pay_status === 2" class="Grid-cell text-right" style="color: #f56c6c;">已支付（已退）</view>
										</view>
										<view class="Grid">
											<view style="max-width: 100px;">{{item.nickname}}</view>
											<view class="Grid-cell text-right">{{item.update_time}}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</z-paging>
				</view>
			</u-popup>
			<!-- 购买记录 -->
			<u-popup :show="isOrderList" @close="isOrderList = false" closeable mode="right" safeAreaInsetTop>
				<view class="text-xs" style="width: 70vw; padding: 0 15px;">
					<!-- 虚拟列表 -->
					<!-- 如果页面中的cell高度是固定不变的，则不需要设置cell-height-mode，如果页面中高度是动态改变的，则设置cell-height-mode="dynamic" -->
					<z-paging unit="px" ref="orderListPaging" :fixed="false" :height="zPagingHeight" use-virtual-list @query="getOrderList">
						<!-- 通过slot="cell"插入列表for循环的cell，slot-scope中提供当前for循环的item和index -->
						<!-- 因字节跳动小程序不支持slot-scope，因此不支持字节跳动小程序 -->
						<view slot="cell" slot-scope="{item,index}">
							<view class="bg-white press-down-active" style="padding: 5px 0; margin-bottom: 10px; border-radius: 4px;">
								<view class="Media">
									<view style="width: 45px; height: 45px; position: relative;">
										<u--image shape="circle" :src="item.headimgurl" width="45px" height="45px" @click="() => {
											data = item
											$refs.xfmx.open()
										}" />
										<u--image v-if="item.is_subscribe" shape="circle" :src="wx_png" width="24" height="24" style="position: absolute; bottom: 0; right: 0;" />
									</view>
									<view class="Media-body text-xs" style="padding: 0 10px;">
										<view class="Grid">
											<view style="margin-bottom: 10px;">ID:{{item.member_id}}</view>
											<view class="Grid-cell text-right">&yen;{{item.pay_real}}{{payTypeMap[item.pay_type]}}</view>
										</view>
										<view class="Grid">
											<view style="max-width: 100px;">{{item.nickname}}</view>
											<view class="Grid-cell text-right">{{item.pay_time}}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</z-paging>
				</view>
			</u-popup>
			<!-- 设置秒杀虚拟库存 -->
			<u-modal :show="isVirtualInventory" title="调整秒杀虚拟库存" confirmText="确定" showCancelButton @confirm="setVirtualInventoryConfirm" @cancel="() => isVirtualInventory = false">
				<u--form labelPosition="left" :model="virtualInventoryForm" ref="uForm" errorType="message">
					<u--input v-model="virtualInventoryForm.surplus_show" type="number" placeholder="请设置秒杀虚拟库存"></u--input>
				</u--form>
			</u-modal>
			<!-- 消费记录 -->
			<xfmx-popup ref="xfmx" :data="data" />
		</z-paging-swiper>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'
	import SwiperListItem from '@/components/swiper-list-item/swiper-list-item.vue';
	import {
		fansList,
		getContentList,
		getDisable,
		getIstop,
		getDele,
		editdiscount,
		getContentLog,
		getOrderList,
		setVirtualInventory,
		restoreArticle
	} from '@/service/index.js'
	import xfmxPopup from '@/components/xfmx-popup/xfmx-popup.vue'
	import wx_png from '@/static/images/my/wx.png'

	export default {
		name: 'home',
		provide() {
			return {
				callDisable: this.getDisable,
				callIstop: this.getIstop,
				callDele: this.getDele,
				callDiscount: this.handleDiscount,
				callContentLog: this.handleContentLog,
				callOrderList: this.handleOrderList,
				callVirtualInventory: this.handleVirtualInventory,
				callRestoreArticle: this.handleRestoreArticle,
				callEditSkm: this.handleEditSkm
			}
		},
		components: {
			SwiperListItem,
			// poster,
			xfmxPopup
		},
		props: {
			visible: {
				type: Boolean,
				default: false
			},
		},
		computed: {
			...mapState({
				token: state => state.login.token,
				userInfo: state => state.login.userInfo
			}),
			getText() {
				if (this.queryField === 'member_id') return '会员ID'
				if (this.queryField === 'nickname') return '昵称'
			}
		},
		watch: {
			token() {
				this.isView = true
			},
			visible() {
				this.visible && this.initData()
			}
		},
		data() {
			return {
				wx_png,
				// 虚拟列表高度
				zPagingHeight: `${uni.getSystemInfoSync().windowHeight - 100}px`,
				isUsers: false,
				isContentLog: false,
				isOrderList: false,
				clickIndex: 0,
				keyword: '', //搜索值
				tabList: [{name: '已发布', value: 0}, {name: '草稿箱', value: 1}, {name: '模板', value: 3}, {name: '回收站', value: 2}, {name: '收款码', value: 4}],
				current: 0, // tabs组件的current值，表示当前活动的tab选项
				orderNumTotal: {},
				generate: ``,
				path: ``,
				posterImage: ``,

				// 设置折扣
				discountShow: false,
				form: {
					discountList: [{
						price: '',
						user_ids: []
					}]
				},
				rules: {},
				// form-label-style
				formLabelStyle: {
					fontSize: '12px',
					color: '#2D3338',
					paddingLeft: '12px',
				},
				fansShow: false,
				fansData: [],
				fansCheck: [],
				fansKeyword: '',
				// 当前操作选择粉丝索引
				selectFansIndex: 0,
				// 筛选条件选择
				isQueryField: false,
				queryField: 'nickname',
				isView: false,
				payStatusMap: {
					0: '未支付',
					1: '已支付',
					2: '已支付（已退）'
				},
				payTypeMap: {
					1: '微信',
					2: '支付宝',
					3: '其他'
				},
				isVirtualInventory: false,
				virtualInventoryForm: {
					content_id: '',
					surplus_show: ''
				},
				data: {},
				user_obj: {
					nickname: '组员',
					user_id: ''
				}
			}
		},
		created() {

		},
		mounted() {
			this.initData()
		},
		methods: {
			handleEditSkm() {
				console.log(1)
			},
			initData() {
				if (this.token) this.isView = true
				// 销毁
				uni.$off("clickIndex")
				// 监听
				uni.$on('clickIndex', (index) => {
					this.clickIndex = index
				})
			},
			// 循环设置rules
			setRules() {
				this.form.discountList.forEach((ele, index) => {
					this.$set(this.rules, `discountList.${index}.price`, [{
						required: true,
						message: '输入金额有误!',
						validator: (rule, value, callback) => {
							if (value === '') return false
							return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value) ||
								value == 0;
						},
						trigger: 'blur'
					}])
					this.$set(this.rules, `discountList.${index}.user_ids`, [{
						required: true,
						validator: (rule, value, callback) => {
							return value.length ? true : false
						},
						message: '请选择关联用户',
						trigger: ['blur', 'change']
					}])
				})
				this.$nextTick(() => {
					this.$refs.uForm.setRules(this.rules)
				})
			},
			// 数据加载
			loadData(params) {
				params.draft_status = this.tabList[this.current].value
				params.user_id = this.user_obj.user_id ? this.user_obj.user_id : this.userInfo.user_id
				// params.wechat_id = this.userinfo.wechat_info.wechat_id
				getContentList({
					params
				}).then(res => {
					this.orderNumTotal = res.data.total
					this.$refs.listItem[this.current].getData(res.data.list)
				})
			},
			// 搜索
			search() {
				let params = {
					draft_status: this.tabList[this.current].value,
					search_value: this.keyword,
					search_field: 'name',
					search_exp: 'like',
					page: 1,
					limit: 10
				}
				this.loadData(params)
			},
			// 上下架
			getDisable(params) {
				getDisable((params)).then(res => {
					if (res.code == 200) {
						this.refreshData()
						setTimeout(function() {
							uni.showToast({
								title: params.is_disable == 1 ? '已下架' : '成功上架',
								icon: 'none'
							})
						}, 1000);
					}
				})
			},
			// 置顶
			getIstop(params) {
				getIstop((params)).then(res => {
					if (res.code == 200) {
						this.refreshData()
						setTimeout(function() {
							uni.showToast({
								title: params.is_top == 1 ? '成功置顶' : '已取消',
								icon: 'none'
							})
						}, 1000);
					}
				})
			},
			// 删除
			getDele(params) {
				getDele((params)).then(res => {
					if (res.code == 200) {
						this.refreshData()
						setTimeout(function() {
							uni.showToast({
								title: '删除成功',
								icon: 'none'
							})
						}, 1000);
					}
				})
			},
			handleClick(index) {
				this.$emit('change', index)
			},
			// tabs通知swiper切换
			tabsChange(index, value) {
				this.current = index;
			},
			// swiper滑动中
			swiperTransition(e) {
				this.$refs.tabs.setDx(e.detail.dx);
			},
			// swiper滑动结束
			swiperAnimationfinish(e) {
				this.current = e.detail.current;
				this.$refs.tabs.unlockDx();
			},
			// 如果要通知当前展示的z-paging刷新，请调用此方法
			reloadCurrentList() {
				this.$refs.listItem[this.current].reload();
			},
			saveImg() {
				uni.downloadFile({
					url: this.posterImage,
					success: (res) => {
						if (res.statusCode === 200) {

						}
					}
				});
			},
			// 设置折扣
			handleDiscount(e) {
				this.colContent = e
				// 初始化数据
				let map = this.colContent.discount ? JSON.parse(this.colContent.discount) : {
					'': []
				}
				let arr = []
				Object.keys(map).forEach((key) => {
					arr.push({
						price: key,
						user_ids: map[key]
					})
				})
				this.form.discountList = arr

				this.setRules()
				this.discountShow = true
			},
			fansChange(e, item) {
				if (e) {
					this.form.discountList[this.selectFansIndex].user_ids.push(JSON.parse(item.value))
				} else {
					// 过滤掉取消勾选的项
					this.form.discountList[this.selectFansIndex].user_ids = this.form.discountList[this.selectFansIndex]
						.user_ids.filter(v => v.member_id !== JSON.parse(item.value)
							.member_id)
				}
				this.$forceUpdate()
			},
			async getFansList() {
				// 拼查询条件
				if (this.fansKeyword) {
					this.query[this.queryField] = this.fansKeyword
				} else {
					this.query[this.queryField] = ''
				}
				// 初始化this.fansCheck
				let v = []
				for (let i = 0; i < this.form.discountList[this.selectFansIndex].user_ids.length; i++) {
					v.push(JSON.stringify(this.form.discountList[this.selectFansIndex].user_ids[i]))
				}
				this.fansCheck = v

				// 将已选的用户id带入查询接口，做排除处理
				let arr = []
				this.form.discountList.forEach((item, index) => {
					// 排除当前this.selectFansIndex的列
					if (this.selectFansIndex !== index) {
						item.user_ids.forEach((v) => {
							arr.push(v.member_id)
						})
					}
				})
				// 获取去重后数组的member_id集合，并带入参数[]进行查询
				const uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index)
				this.query.not_in = uniqueArr

				const result = await fansList(this.query)

				if (result.code === 200) {
					let arr = []
					for (let i = 0; i < result.data.list.length; i++) {
						arr.push({
							text: `ID:${result.data.list[i].member_id},${result.data.list[i].nickname}`,
							value: JSON.stringify(result.data.list[i])
						})
					}
					this.fansData = this.fansData.concat(arr)
					this.query.page++
				}
			},
			// 点击关联用户
			relation(index) {
				this.selectFansIndex = index
				this.search_value = ''
				this.queryField = 'nickname'
				this.query = {
					nickname: '',
					member_id: '',
					wechat_id: '',
					status: [1, 3],
					page: 1,
					limit: 20,
					not_in: []
				}
				this.query.wechat_id = this.colContent.wechat_id
				let arr = []
				for (let i = 0; i < this.form.discountList[index].user_ids.length; i++) {
					arr.push(JSON.stringify(this.form.discountList[index].user_ids[i]))
				}
				this.fansCheck = arr
				this.fansData = []
				this.getFansList()
				this.fansShow = true
			},
			// 完成折扣设置
			handleDiscountOk() {
				this.$refs.uForm.validate().then(res => {
					/*验证通过逻辑*/
					let map = {}
					this.form.discountList.forEach((item) => {
						map[item.price] = item.user_ids
					})
					// 设置
					editdiscount({
							content_id: this.colContent.content_id,
							discount: JSON.stringify(map)
						})
						.then((res) => {
							if (res.code === 200) {
								this.refreshData()
								setTimeout(function() {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}, 1000);
								this.discountShow = false
							}
						})
						.catch(() => {})
				}).catch(errors => {})
			},
			refreshData() {
				this.$refs.listItem[this.current].refresh()
			},
			// 金额千分位过滤器
			thousandSep(params) {
				if (params === null || params === undefined) {
					return null
				} else if (isNaN(params)) {
					return 'NaN'
				} else {
					let num = params.toString()
					if (/^-?\d+\.?\d+$/.test(num)) {
						if (/^-?\d+$/.test(num)) {
							num = num.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
						} else {
							let numarray = num.split('.')
							let numbefore = numarray[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
							num = numbefore + '.' + numarray[1]
						}
					}
					return num
				}
			},
			// 浏览数
			handleContentLog(e) {
				this.colContent = e
				this.isContentLog = true
			},
			// 购买记录
			handleOrderList(e) {
				this.colContent = e
				this.isOrderList = true
			},
			async getContentLog(page, limit) {
				uni.showLoading({
					title: '拉取中'
				});
				try {
					const {
						data: {
							list
						}
					} = await getContentLog({
						page: page,
						limit: limit,
						content_id: this.colContent.content_id
					})
					//将请求的结果数组传递给z-paging
					this.$refs.contentLogPaging.complete(list || []);
				} catch (e) {
					//TODO handle the exception
				}
			},
			async getOrderList(page, limit) {
				uni.showLoading({
					title: '拉取中'
				});
				try {
					const {
						data: {
							list,
							total,
						}
					} = await getOrderList({
						page: page,
						limit: limit,
						status: 1,
						content_id: this.colContent.content_id
					})
					this.$emit('getList', total)
					//将请求的结果数组传递给z-paging
					this.$refs.orderListPaging.complete(list || []);
				} catch (e) {
					//TODO handle the exception
				}
			},
			// 设置秒杀虚拟库存
			handleVirtualInventory(e) {
				this.colContent = e
				this.virtualInventoryForm = {
					content_id: e.content_id,
					surplus_show: e.marketing_tools[5].surplus_show
				}
				this.isVirtualInventory = true
			},
			// 完成设置秒杀虚拟库存
			setVirtualInventoryConfirm() {
				setVirtualInventory(this.virtualInventoryForm).then(res => {
					if (res.code == 200) {
						this.refreshData()
						setTimeout(function() {
							uni.showToast({
								title: '设置成功',
								icon: 'none'
							})
						}, 1000);
						this.isVirtualInventory = false
					} else {
						setTimeout(function() {
							vm.$showToast(res.msg)
						}, 1000);
					}
				})
			},
			// 回收站还原
			handleRestoreArticle(e) {
				restoreArticle({
					content_id: e.content_id
				}).then(res => {
					if (res.code == 200) {
						this.refreshData()
						setTimeout(function() {
							uni.showToast({
								title: '还原成功',
								icon: 'none'
							})
						}, 1000);
					}
				})
			},
			handleUserList() {
				this.isUsers = true
				this.$nextTick(() => {
					this.$refs.userListPaging.complete(this.userInfo.user_list || []);
				})
			}
		},
		beforeDestroy() {
			console.log("-销毁uni.$on clickIndex-")
			uni.$off("clickIndex")
		}
	}
</script>

<style lang="scss" scoped>
	.swiper {
		height: calc(100% - 51px);
		background: $uni-common-bg-color;
		padding: 0 15px 10px;
	}

	.orderNum {
		background: linear-gradient(180deg, rgba(229, 239, 253, 0.5) 0%, #DCE9FD 100%), #FFFFFF;
		border-radius: 8px 8px 8px 8px;
		margin: 8px 0;

		.orderNum-item {
			width: 25%;
			display: inline-block;
			text-align: center;

			.num {
				font-weight: 600;
				font-size: 12px;
				color: #0074FF;
				line-height: 24px;
			}

			.name {
				font-weight: 400;
				font-size: 12px;
				color: #939393;
				line-height: 20px;
			}
		}
	}

	.custom-form {
		background: #FFFFFF;
		border-radius: 8px;
		padding: 0 12px;
	}
	// ::v-deep .u-search__content {
	// 	border-color: #52B36D !important;
	// }
</style>